package mms

class InspectionItemController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [inspectionItemInstanceList: InspectionItem.list(params), inspectionItemInstanceTotal: InspectionItem.count()]
    }

    def create = {
        def inspectionItemInstance = new InspectionItem()
        inspectionItemInstance.properties = params
        return [inspectionItemInstance: inspectionItemInstance]
    }

    def save = {
        def inspectionItemInstance = new InspectionItem(params)
        if (inspectionItemInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'inspectionItem.label', default: 'InspectionItem'), inspectionItemInstance])}"
            redirect(action: "show", id: inspectionItemInstance.id)
        }
        else {
            render(view: "create", model: [inspectionItemInstance: inspectionItemInstance])
        }
    }

    def show = {
        def inspectionItemInstance = InspectionItem.get(params.id)
        if (!inspectionItemInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'inspectionItem.label', default: 'InspectionItem'), params.id])}"
            redirect(action: "list")
        }
        else {
            [inspectionItemInstance: inspectionItemInstance]
        }
    }

    def edit = {
        def inspectionItemInstance = InspectionItem.get(params.id)
        if (!inspectionItemInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'inspectionItem.label', default: 'InspectionItem'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [inspectionItemInstance: inspectionItemInstance]
        }
    }

    def update = {
        def inspectionItemInstance = InspectionItem.get(params.id)
        if (inspectionItemInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (inspectionItemInstance.version > version) {
                    
                    inspectionItemInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'inspectionItem.label', default: 'InspectionItem')] as Object[], "Another user has updated this InspectionItem while you were editing")
                    render(view: "edit", model: [inspectionItemInstance: inspectionItemInstance])
                    return
                }
            }
            inspectionItemInstance.properties = params
            if (!inspectionItemInstance.hasErrors() && inspectionItemInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'inspectionItem.label', default: 'InspectionItem'), inspectionItemInstance])}"
                redirect(action: "show", id: inspectionItemInstance.id)
            }
            else {
                render(view: "edit", model: [inspectionItemInstance: inspectionItemInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'inspectionItem.label', default: 'InspectionItem'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def inspectionItemInstance = InspectionItem.get(params.id)
        if (inspectionItemInstance) {
            try {
                inspectionItemInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'inspectionItem.label', default: 'InspectionItem'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'inspectionItem.label', default: 'InspectionItem'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'inspectionItem.label', default: 'InspectionItem'), params.id])}"
            redirect(action: "list")
        }
    }
}
